// app.js
//import testdata from "../../testdata/testdata.js" //测试用数据
//const testdata = require("./testdata/testdata.js")

App({
  login: function () {
    // 登录
    wx.login({
      success: res => {
        console.log("Get code: ", res.code);
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: 'https://api.fracher21.top/spotit/login',
          method: 'POST',
          data: {
            code: res.code
          },
          success: (res) => {
            console.log("token : " + res.data.token);
            this.globalData.token = res.data.token
            wx.setStorage({
              key: 'token',
              data: res.data.token
            })
          }
        });
      }
    });
  },
  checkLogin: function (callback) {
    var token = this.globalData.token
    if (!token) {
      token = wx.getStorageSync('token')
      if (token) {
        this.globalData.token = token
      } else {
        callback({ is_login: false })
        return
      }
    }
    wx.request({
      url: 'https://api.fracher21.top/spotit/checklogin',
      method: 'GET',
      data: {
        token: token
      },
      success: (res) => {
        console.log(res);
        callback({
          is_login: res.data.is_login
        })
      }
    })
  },
  onLaunch() {
    this.checkLogin(res => {
      console.log('is_login: ', res.is_login);
      if (!res.is_login) {
        this.login();
      }
    })
    this.updateMarkers();

  },

  updateMarkers: function() {
    this.getMarkers()
    .then((markers) => {
      // 标记点数据获取成功后更新页面数据
      this.globalData.markers = markers
    })
    .catch((error) => {
      console.error(error);
    });
  },

  updatecurrentMarker: function(newCurrentMarker) {
    this.globalData.currentMarker = newCurrentMarker;
  },

  updatecurrentMarkerToPost: function(newCurrentMarkerToPost){
    this.globalData.currentMarkerToPost = newCurrentMarkerToPost;
  },

  getMarkers() {
    this.globalData.totalmarkerid = 0;
    // console.log("getMarkers CALL")
    return new Promise((resolve, reject) => {
      let markers = [];
      wx.request({
        url: 'https://api.fracher21.top/spotit/mappins',
        method: "GET",
        success: (res) => {
          for (let item of res.data.data) {
            let marker = this.createMarker(item);
            wx.request({
              url: 'https://api.fracher21.top/spotit/checkstarpost',
              method: "GET",
              data: {
                id: marker.label.post_id,
                token: this.globalData.token 
              },
              success: (res) => {
                marker.label.favor = res.data.result;
              }
            })
            markers.push(marker)
          }
        }
      })
      resolve(markers);
    });
  },

  createMarker(point) {
    let latitude = point.latitude >= 90 ? 89 : (point.latitude <= -90 ? -89 : point.latitude);
    let longitude = point.longitude >= 180 ? 179 : (point.longitude <= -180 ? -179 : point.longitude);
    let desc = point.label.desc;
    let locdesc = point.label.locdesc;
    let head = point.label.head;
    let race = point.label.race;
    let animal_id = point.label.animal_id;
    this.globalData.totalmarkerid++;
    let marker = {
      iconPath: "/images/zcr/location0.png",
      id: this.globalData.totalmarkerid || 0,
      name: point.name || '',
      latitude: latitude,
      longitude: longitude,
      width: 35,
      height: 35,
      show:true,
      label: {
        desc: desc,
        locdesc: locdesc,
        head: head,
        post_id: point.id,
        favor: false,
        race: race,
        animal_id: animal_id
      },
    };
    return marker
  },

  globalData: {
    userInfo: null,
    currentMarker: 0,
    currentMarkerToPost: 0,
    markers: [],
    totalmarkerid: 0,
    //selectOpt:'',
    //postsOnshow:[],   //动态页面展示的帖子数据
    //postsOnshow:[],
  }
})
